import axios from '../plugins/axios';

// 时间配置类型
export interface TimeConfig {
  id?: number;
  configType: 'WORKING_HOURS' | 'INTERVAL';
  name: string;
  startTime?: string;
  endTime?: string;
  intervalMinutes?: number;
  duration?: number;
  days?: string;
  description?: string;
  isActive: boolean;
  createdTime?: string;
  updatedTime?: string;
}

// 获取所有时间配置
export const getTimeConfigs = async () => {
  try {
    const response = await axios.get('/api/timeConfig/list.do');
    return response.data;
  } catch (error) {
    console.error('获取时间配置失败：', error);
    throw error;
  }
};

// 根据配置类型获取时间配置
export const getTimeConfigsByType = async (configType: string) => {
  try {
    const response = await axios.get(`/api/timeConfig/listByType.do?configType=${configType}`);
    return response.data;
  } catch (error) {
    console.error('获取时间配置失败：', error);
    throw error;
  }
};

// 获取启用的工作时间配置
export const getActiveWorkingHours = async () => {
  try {
    const response = await axios.get('/api/timeConfig/getActiveWorkingHours.do');
    return response.data;
  } catch (error) {
    console.error('获取工作时间配置失败：', error);
    throw error;
  }
};

// 获取启用的间隔时间配置
export const getActiveIntervals = async () => {
  try {
    const response = await axios.get('/api/timeConfig/getActiveIntervals.do');
    return response.data;
  } catch (error) {
    console.error('获取间隔时间配置失败：', error);
    throw error;
  }
};

// 根据ID获取时间配置
export const getTimeConfigById = async (id: number) => {
  try {
    const response = await axios.get(`/api/timeConfig/get.do?id=${id}`);
    return response.data;
  } catch (error) {
    console.error('获取时间配置失败：', error);
    throw error;
  }
};

// 添加时间配置
export const addTimeConfig = async (timeConfig: TimeConfig) => {
  try {
    const response = await axios.post('/api/timeConfig/add.do', timeConfig);
    return response.data;
  } catch (error) {
    console.error('添加时间配置失败：', error);
    throw error;
  }
};

// 更新时间配置
export const updateTimeConfig = async (timeConfig: TimeConfig) => {
  try {
    const response = await axios.post('/api/timeConfig/update.do', timeConfig);
    return response.data;
  } catch (error) {
    console.error('更新时间配置失败：', error);
    throw error;
  }
};

// 删除时间配置
export const deleteTimeConfig = async (id: number) => {
  try {
    const response = await axios.get(`/api/timeConfig/delete.do?id=${id}`);
    return response.data;
  } catch (error) {
    console.error('删除时间配置失败：', error);
    throw error;
  }
};

// 批量更新启用状态
export const updateTimeConfigStatus = async (ids: number[], isActive: boolean) => {
  try {
    const response = await axios.post('/api/timeConfig/updateStatus.do', { ids, isActive });
    return response.data;
  } catch (error) {
    console.error('更新状态失败：', error);
    throw error;
  }
};

// 创建默认时间配置
export const createDefaultTimeConfigs = async () => {
  try {
    const response = await axios.get('/api/timeConfig/createDefault.do');
    return response.data;
  } catch (error) {
    console.error('创建默认时间配置失败：', error);
    throw error;
  }
};

// 获取时间配置的Map格式数据
export const getTimeConfigMaps = async (configType: string) => {
  try {
    const response = await axios.get(`/timeConfig/getMaps.do?configType=${configType}`);
    return response.data;
  } catch (error) {
    console.error('获取时间配置失败：', error);
    throw error;
  }
};

// 工具函数：解析工作日字符串
export const parseWorkingDays = (days: string): number[] => {
  if (!days) return [];
  return days.split(',').map(day => parseInt(day.trim()));
};

// 工具函数：格式化工作日显示
export const formatWorkingDays = (days: string): string => {
  const dayNames = ['', '周一', '周二', '周三', '周四', '周五', '周六', '周日'];
  const dayNumbers = parseWorkingDays(days);
  return dayNumbers.map(day => {
    if (day >= 1 && day <= 7) {
      return dayNames[day];
    }
    return '';
  }).filter(Boolean).join(', ');
};

// 工具函数：生成工作小时数组
export const generateWorkingHours = (startTime: string, endTime: string): number[] => {
  const [startHour] = startTime.split(':').map(Number);
  const [endHour] = endTime.split(':').map(Number);
  const hours: number[] = [];
  for (let hour = startHour; hour < endHour; hour++) {
    hours.push(hour);
  }
  return hours;
};

// 工具函数：验证时间格式
export const validateTimeFormat = (time: string): boolean => {
  const timeRegex = /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/;
  return timeRegex.test(time);
};

// 工具函数：比较时间
export const compareTime = (time1: string, time2: string): number => {
  const [h1, m1] = time1.split(':').map(Number);
  const [h2, m2] = time2.split(':').map(Number);
  const total1 = h1 * 60 + m1;
  const total2 = h2 * 60 + m2;
  return total1 - total2;
}; 